var express = require("express");
var router = express.Router();
var leaveService = require("../service/leaveService");
let jwt = require("jsonwebtoken");

// 新增请假接口
router.post("/add", function (req, res, next) {
  //接收前端传来的数据  写入数据库  返回值
  var token = req.headers.token; //解析token
  jwt.verify(token, "key", async (err, code) => {
    if (!err) {
      if (code.usertype == 2) {
        let userid = code.id;
        let username = code.username; //从token里面取出用户编号和用户名
        var { leavereason, start, end, extend } = req.body;
        let result = await leaveService.add({
          userid,
          username,
          leavereason,
          start,
          extend,
          end,
          type: 1,
        }); //默认 待审核状态1 同意2 拒绝0
        res.json({
          code: 200,
          msg: "请假成功",
          result,
        });
      } else {
        res.json({
          code: 2,
          msg: "权限不够 非法使用",
        });
      }
    } else {
      res.json({
        code: 0,
        msg: "token异常",
      });
    }
  });
});
// 根据身份查询请假列表

router.get("/list", (req, res, next) => {
  let token = req.headers.token;
  let { shownum, pageindex } = req.query; //接受前端传来的参数
  jwt.verify(token, "key", async (err, info) => {
    if (info.usertype == 2) {
      let userid = info.id;
      leaveService.query({ userid }).then((result) => {
        res.json({
          code: 200,
          list: result,
        });
      });
    } else if (err || info.usertype != 1) {
      res.json({
        code: 203,
        msg: "token或者权限异常",
      });
    } else {
      //查询哪一页的数据
      //并且返回总数
      let result = await leaveService.query(
        {},
        (pageindex - 1) * shownum,
        shownum * 1
      ); //显示第一页的数据
      let total = await leaveService.querycount({});
      res.json({
        code: 200,
        list: result,
        total: total, //表示数据总数
      });
    }
  });
});
// 更改请假状态
router.get("/changetype", (req, res, next) => {
  //根据_id更新type
  if (req.headers && req.headers.token) {
    var token = req.headers.token; //验证token    判断权限    在更新
    jwt.verify(token, "key", function (err, code) {
      if (!err) {
        if (code.usertype == 1) {
          //code.usertype表示权限
          var { _id, type } = req.query; //type表示 类别
          leaveService.update({ _id: Object(_id) }, { $set: { type } }).then(
            (result) => {
              if (result.nModified) {
                res.json({
                  code: 200,
                  msg: "更新成功",
                });
              } else {
                res.json({
                  code: 0,
                  msg: "更新失败",
                });
              }
            },
            () => {
              res.json({
                code: 0,
                msg: "更新失败",
              });
            }
          );
        } else {
          res.json({
            code: 2,
            msg: "token异常 权限不够",
          });
        }
      } else {
        res.json({
          code: 2,
          msg: "token异常",
        });
      }
    });
  } else {
    res.json({
      code: 0,
      msg: "token缺失",
    });
  }
});
//分组查询
router.get("/group", async (req, res, next) => {
  //应该要验证token 省略
  let { key } = req.query; //get请求  post才是body
  let result = await leaveService.group(key);
  res.json({
    code: 200,
    result,
  });
});

module.exports = router;
